package com.retch.wenchengmao.sqlitedeme2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * author:wenchengmao
 * time:  2017/3/20.
 * email: wenchengmao@yeah.net
 */

public class ModifyActivity extends Activity{
    private EditText prices,names,tuitions;
    private Button modify;
    int id;
    String name;
    double tuition;
    float price;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_data);
        initView();
        getIntentData();
        click();
    }

    public void initView()
    {
        prices=(EditText)findViewById(R.id.price);
        names=(EditText)findViewById(R.id.name);
        tuitions=(EditText)findViewById(R.id.tuition);
        modify=(Button)findViewById(R.id.modify);
    }

    public void getIntentData()
    {
        Intent intent=getIntent();
        id=intent.getExtras().getInt("id");
        name=intent.getExtras().getString("name");
        tuition=intent.getExtras().getDouble("tuition");
        price=intent.getExtras().getFloat("price");
        names.setText(name);
        tuitions.setText(String.valueOf(tuition));
        prices.setText(String.valueOf(price));
    }

    public void click()
    {
        modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upData();
                modify();
                Toast.makeText(ModifyActivity.this,"修改成功!",Toast.LENGTH_LONG).show();
                finish();
            }
        });
    }

    public void modify()
    {
        Student student=new Student(id,name,tuition,price);
        MySQLiteHelper mySQLiteHelper= new MySQLiteHelper(ModifyActivity.this, "db_test5", null, 1);
        mySQLiteHelper.upDate(student.getClass(),student,id);
    }

    public void upData()
    {
        name=names.getText().toString().trim();
        tuition=Double.parseDouble(tuitions.getText().toString().trim());
        price=Float.parseFloat(prices.getText().toString().trim());
    }

}
